package com.hodgepodge.commons.validator.constraint;

import com.hodgepodge.commons.validator.annotation.FixedNumValues;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * title FixedNumValuesValidator
 * projectName colorful-blog
 * desc 在固定数字值范围内校验
 *
 * @author jiangjian
 * @date 2021/6/2 17:42
 */
public class FixedNumValuesValidator implements ConstraintValidator<FixedNumValues, Integer> {

    private int[] values;

    @Override
    public void initialize(FixedNumValues fixedNumValues) {
        this.values = fixedNumValues.values();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return value == null || Arrays.stream(values).anyMatch(obj -> Integer.valueOf(obj).equals(value));
    }
}
